package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    /**
     * 完成运维工单,修改售货机状态
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {

        TaskCompleteContract contract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        LambdaQueryWrapper<VendingMachineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendingMachineEntity::getInnerCode, contract.getInnerCode());
        VendingMachineEntity entity = vendingMachineService.getOne(queryWrapper);
        if (contract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
            entity.setVmStatus(VMSystem.VM_STATUS_RUNNING);
        } else if (contract.getTaskType() == VMSystem.TASK_TYPE_REPAIR) {
            entity.setVmStatus(VMSystem.VM_STATUS_RUNNING);
        } else if (contract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            entity.setVmStatus(VMSystem.VM_STATUS_REVOKE);
        }
        vendingMachineService.updateById(entity);

    }
}
